{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-09-23T05:29:24.966586Z",
     "start_time": "2025-09-23T05:29:20.363428Z"
    }
   },
   "source": [
    "from selenium import  webdriver\n",
    "from selenium.webdriver.common.by import By\n",
    "driver = webdriver.Chrome()\n",
    "driver.implicitly_wait(5)\n",
    "\n",
    "driver.get('https://www.byhy.net/cdn2/files/selenium/test1.html')\n",
    "\n",
    "# 绝对路径选择\n",
    "elements = driver.find_elements(By.XPATH, \"/html/body/div/select\")\n",
    "print(len(elements))\n",
    "for element in elements:\n",
    "    print(element.text)\n",
    "\n",
    "# 相对路径选择\n",
    "elements = driver.find_elements(By.XPATH, \"//div//p\")\n",
    "print(len(elements))\n",
    "for element in elements:\n",
    "    print(element.text)"
   ],
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Exception managing chrome: error sending request for url (https://googlechromelabs.github.io/chrome-for-testing/known-good-versions-with-downloads.json)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "    小江老师\n",
      "    小雷老师\n",
      "    小凯老师\n",
      "小江老师\n",
      "小雷老师\n",
      "小王老师\n",
      "小凯老师\n",
      "8\n",
      "姓名:\n",
      "课程:\n",
      "城市选择\n",
      "北京\n",
      "上海\n",
      "纽约\n",
      "休斯顿\n",
      "芝加哥\n"
     ]
    }
   ],
   "execution_count": 1
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-23T05:31:29.525636Z",
     "start_time": "2025-09-23T05:31:28.821347Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 通配符\n",
    "elements = driver.find_elements(By.XPATH, \"//div/*\")\n",
    "print(len(elements))\n",
    "for element in elements:\n",
    "    print(element.text)"
   ],
   "id": "fde85a2dc223d0f7",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "16\n",
      "单选\n",
      "姓名:\n",
      "    小江老师\n",
      "    小雷老师\n",
      "    小凯老师\n",
      "\n",
      "多选\n",
      "课程:\n",
      "小江老师\n",
      "小雷老师\n",
      "小王老师\n",
      "小凯老师\n",
      "\n",
      "城市选择\n",
      "北京\n",
      "上海\n",
      "纽约\n",
      "休斯顿\n",
      "芝加哥\n",
      "城市选择\n",
      "北京\n",
      "上海\n",
      "北京\n",
      "上海\n",
      "纽约\n",
      "休斯顿\n",
      "芝加哥\n",
      "纽约\n",
      "休斯顿\n",
      "芝加哥\n"
     ]
    }
   ],
   "execution_count": 2
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 根据属性选择",
   "id": "860f2b5ac015d2a3"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "# 选择id为west的页面元素\n",
    "elements = driver.find_elements(By.XPATH, \"//div[@id='west']\")\n",
    "# 选择class为multi_choice的页面元素\n",
    "elements = driver.find_elements(By.XPATH, \"//select[@class='multi_choice']\")\n",
    "# 选择style中包含color字符串的页面元素\n",
    "elements = driver.find_elements(By.XPATH, \"//*[contains(@style, 'color')]\")\n",
    "# 选择style中以color开头的页面元素\n",
    "elements = driver.find_elements(By.XPATH, \"//*[starts-with(@style, 'color')]\")"
   ],
   "id": "771291217628a098"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 按次序选择",
   "id": "6d5a561db5e15826"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "# 选择父元素为div中的p类型 第2个子元素\n",
    "elements = driver.find_elements(By.XPATH, \"//div/p[2]\")\n",
    "\n",
    "# 第几个子元素\n",
    "elements = driver.find_elements(By.XPATH, \"//div/*[2]\")\n",
    "# 某类型倒数第几个元素\n",
    "elements = driver.find_elements(By.XPATH, \"//div/p[last()-1]\")\n",
    "\n",
    "# 范围选择\n",
    "elements = driver.find_elements(By.XPATH, \"//div/p[1<=position()<=2]\")\n",
    "\n",
    "# 选择class属性为multi_choice的页面元素中，前3个元素\n",
    "elements = driver.find_elements((By.XPATH, \"//*[@class='multi_choice']/*[position()<=3]\"))"
   ],
   "id": "b1ed48f79b1cfac6"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 组选择、父节点、兄弟节点",
   "id": "f0c222b04d430d98"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "# 等价于CSS中 option, h4\n",
    "elements = driver.find_elements(By.XPATH, \"//option | //h4\")\n",
    "\n",
    "# 选择父节点\n",
    "# 选择id为 china 的节点的父节点\n",
    "elements = driver.find_elements(By.XPATH, \"//*[@id='china']/..\")\n",
    "\n",
    "# 选择 class 为 single_choice 的元素的 所有 前面的兄弟节点\n",
    "elements = driver.find_elements(By.XPATH, \"//*[@class='single_choice']/preceding-sibling::*\")"
   ],
   "id": "3a07f40423f53cf6"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-23T05:55:00.979048Z",
     "start_time": "2025-09-23T05:54:55.306731Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from selenium import  webdriver\n",
    "from selenium.webdriver.common.by import By\n",
    "driver = webdriver.Chrome()\n",
    "driver.implicitly_wait(5)\n",
    "\n",
    "driver.get('https://www.byhy.net/cdn2/files/selenium/test1.html')\n",
    "\n",
    "# 先寻找id是china的元素\n",
    "china = driver.find_element(By.ID, 'china')\n",
    "\n",
    "# 再选择该元素内部的p元素\n",
    "elements = china.find_elements(By.XPATH, './/p')\n",
    "\n",
    "# 打印结果\n",
    "for element in elements:\n",
    "    print('----------------')\n",
    "    print(element.get_attribute('outerHTML'))\n",
    "\n",
    "# 结论：在某个元素内部使xpath选择元素，需要在xpath表达式最前面加个点"
   ],
   "id": "edaa6ddf0cd95f5e",
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Exception managing chrome: error sending request for url (https://googlechromelabs.github.io/chrome-for-testing/known-good-versions-with-downloads.json)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "----------------\n",
      "<p id=\"beijing\" class=\"capital huge-city\">\n",
      "            北京    \n",
      "        </p>\n",
      "----------------\n",
      "<p id=\"shanghai\" class=\"huge-city\">\n",
      "            上海    \n",
      "        </p>\n"
     ]
    }
   ],
   "execution_count": 4
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": "",
   "id": "f5a6a010efee4d47"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
